<?php


namespace app\admin\controller\webconfig;

use app\admin\controller\Base;
use app\common\model\WebConfig as WebConfigModel;
use think\facade\View;

class WebConfig extends Base
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new WebConfigModel;
    }

    public function index()
    {
        $siteList = [];
        $groupList = WebConfigModel::getGroupList();
        foreach ($groupList as $k => $v) {
            $siteList[$k]['name'] = $k;
            $siteList[$k]['title'] = $v;
            $siteList[$k]['list'] = [];
        }
        $allData = $this->model->select();
        foreach ($allData as $k => $v) {
            if (!isset($siteList[$v['group']])) {
                continue;
            }
            $value = $v->toArray();
            if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                $value['value'] = explode(',', $value['value']);
            }
            $value['content'] = json_decode($value['content'], true);
            $value['tip'] = htmlspecialchars($value['tip']);
            $siteList[$v['group']]['list'][] = $value;
        }
        $index = 0;
        foreach ($siteList as $k => &$v) {
            $v['active'] = !$index ? true : false;
            $index++;
        }
        $assignData = [
            'site_list'  => $siteList,
            'type_list'  => WebConfigModel::getTypeList(),
            'rule_list'  => WebConfigModel::getRegexList(),
            'group_list' => WebConfigModel::getGroupList()
        ];
        View::assign($assignData);
        return View::fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'trim');
            if ($params) {
                foreach ($params as $k => &$v) {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
                    $params['content'] = json_encode(WebConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
                } else {
                    $params['content'] = '';
                }

                $result = $this->model->save($params);
                if ($result) {
                    $this->success('创建成功');
                } else {
                    $this->error($this->model->getError());
                }
            }
            $this->error('参数异常');
        }
    }

    public function edit($ids=null){
        if ($this->request->isPost()) {
            $row = $this->request->post("row/a", [], 'trim');
            if ($row) {
                $configList = [];
                $allData = $this->model->select();
                foreach ($allData as $v) {
                    if (isset($row[$v['name']])) {
                        $value = $row[$v['name']];
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }
                        $v['value'] = $value;
                        $configList[] = $v->toArray();
                    }
                }
                $result = $this->model->saveAll($configList);
                if ($result) {
                    $this->success('更新成功');
                }else{
                    $this->error('修改失败');
                }

            }
            $this->error('参数异常');
        }
    }
}
